import 'dart:convert';

class MqttMessageData {
  final int receivedTime;

  final String topic;
  final List<int> message;

  MqttMessageData({
    required this.topic,
    required this.message,
    int? time,
  }) : receivedTime = time ?? DateTime.now().millisecondsSinceEpoch;

  MqttMessageData copyWith({
    int? receivedTime,
    String? topic,
    List<int>? message,
  }) {
    return MqttMessageData(
      topic: topic ?? this.topic,
      message: message ?? this.message,
      time: receivedTime ?? this.receivedTime,
    );
  }

  @override
  String toString() {
    return 'MqttMessage(receivedTime: $receivedTime, topic: $topic, message: $message)';
  }

  @override
  bool operator ==(Object other) {
    if (identical(this, other)) return true;

    return other is MqttMessageData && other.receivedTime == receivedTime && other.topic == topic && other.message == message;
  }

  @override
  int get hashCode => receivedTime.hashCode ^ topic.hashCode ^ message.hashCode;

  //message 消息转换成json
  Map<String, dynamic> toMsgJson() {
    //List<int> 转字符串
    String msg = utf8.decode(message);
    return json.decode(msg);
  }
}
